package DAL;

import Entities.TblCurriculum;
import DatabaseAccess.SQLHelper;
import java.sql.ResultSet;
import java.util.*;

public class CurriculumDAL {

    private ResultSet objResult;
    public List<TblCurriculum> list;

    public CurriculumDAL() {
    }

    public boolean add(TblCurriculum curriculum) {
        int result = 0;
        String[] params = new String[1];
        params[0] = curriculum.getCurriculumName();

        try {
            result = SQLHelper.executeUpdate("procInsertCurriculum", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());            
        }
        
        return result == 1 ? true : false;
    }

    public boolean update(TblCurriculum curriculum) {
        int result = 0;
        String[] params = new String[2];
        params[0] = String.valueOf(curriculum.getCurriculumId());
        params[1] = curriculum.getCurriculumName();

        try {
            result = SQLHelper.executeUpdate("procUpdateCurriculum", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());            
        }

        return result == 1 ? true : false;
    }

    public boolean delete(TblCurriculum curriculum) {
        int result = 0;
        String[] params = new String[1];
        params[0] = String.valueOf(curriculum.getCurriculumId());

        try {
            result = SQLHelper.executeUpdate("procDeleteCurriculum", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public List<TblCurriculum> getAll() {
        list = new ArrayList<TblCurriculum>();
        try {
            objResult = SQLHelper.executeQuery("procGetAllCurriculum", null);
            while (objResult.next()) {
                TblCurriculum curriculum = new TblCurriculum();
                curriculum.setCurriculumId(objResult.getInt("CurriculumId"));
                curriculum.setCurriculumName(objResult.getString("CurriculumName"));

                list.add(curriculum);
            }

            return list;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }

    public TblCurriculum getById(int id) {
        TblCurriculum curriculum = new TblCurriculum();
        String[] params = new String[1];
        params[0] = String.valueOf(id);
        try {
            objResult = SQLHelper.executeQuery("procGetCurriculumById", params);
            while (objResult.next()) {
                curriculum.setCurriculumId(objResult.getInt("CurriculumId"));
                curriculum.setCurriculumName(objResult.getString("CurriculumName"));
            }

            return curriculum;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }
}
